﻿Imports System
Imports System.Collections.Generic
Imports System.Text

'Imports System.Windows.Controls
Imports System.Windows.Forms
Imports System.Windows

Imports Microsoft.Ink
Imports Microsoft.StylusInput

Namespace Components

    Public Class InkControl
        Inherits System.Windows.Forms.UserControl
        Private mText As String = ""

        Public Event TextRecognized(Text As String)

        Private WithEvents mTimer As Windows.Forms.Timer

        ' Create a global ink collector object called myInkCollector
        Dim WithEvents myInkCollector As InkCollector

        ' Recognizers is used to retrieve the number of installed recognizers
        Dim myRecognizers As Recognizers


        Public Overrides Property Text As String
            Get
                Return mText
            End Get
            Set(value As String)
                mText = value
            End Set
        End Property


        Public Sub New()

        End Sub

        ''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Event Handler from Form Load Event
        ''''''''''''''''''''''''''''''''''''''''''''''''''
        Private Sub InkRecognition_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            mTimer = New Windows.Forms.Timer
            mTimer.Interval = 1000.0#
            mTimer.Enabled = False

            ' Create the recognizers collection
            myRecognizers = New Recognizers()

            'Create a new ink collector that uses the the group box handle
            myInkCollector = New InkCollector(Me.Handle)

            ' Turn the ink collector on
            myInkCollector.Enabled = True

        End Sub

        ''''''''''''''''''''''''''''''''''''''''''''''''''
        ' Event Handle from Recognize Button Click Event
        ''''''''''''''''''''''''''''''''''''''''''''''''''
        Private Sub AutoRecognize()

            ' Check to ensure that the user has at least one recognizer installed
            ' Note that this is a preventive check - otherwise, an exception will
            ' occur during recognition
            If 0 = myRecognizers.Count Then
                MsgBox("There are no handwriting recognizers installed.")
            Else

                ' Note that the Strokes' ToString() method is a
                ' shortcut  for retrieving the best match using the
                ' default recognizer.  The same result can also be
                ' obtained using the RecognizerContext.  For an
                ' example, uncomment the following lines of code:
                '
                ' Dim myRecoContext As New RecognizerContext()
                ' Dim status As RecognitionStatus
                ' Dim recoResult As RecognitionResult
                '
                ' myRecoContext.Strokes = myInkCollector.Ink.Strokes
                ' recoResult = myRecoContext.Recognize(status)
                ' txtResults.SelectedText = recoResult.TopString
                '
                mText = myInkCollector.Ink.Strokes.ToString
                RaiseEvent TextRecognized(mText)

                ' If the mouse is pressed, do not perform the recognition -
                ' this prevents deleting a stroke that is still being drawn
                If Not myInkCollector.CollectingInk Then

                    ' Delete the ink from the ink collector
                    'myInkCollector.Ink.DeleteStrokes(myInkCollector.Ink.Strokes)

                    ' Force the Frame to redraw (so the deleted ink will go away)
                    Me.Refresh()
                    MsgBox("So far you have: " & mText)
                End If
            End If
        End Sub

        Private Sub mTimer_Tick(sender As Object, e As System.EventArgs) Handles mTimer.Tick
            mTimer.Stop()
            Me.AutoRecognize()
        End Sub

        Private Sub myInkCollector_Stroke(sender As Object, e As Microsoft.Ink.InkCollectorStrokeEventArgs) Handles myInkCollector.Stroke

            mTimer.Enabled = True
            mTimer.Stop()
            mTimer.Start()


        End Sub
    End Class

End Namespace
